package com.jiazhong.mingxing.boot.boot11.事务消息;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;

@Slf4j
public class 事务消息生产 {

    @SneakyThrows
    public static void main(String[] args) {
        TransactionMQProducer producer = new TransactionMQProducer("tx_transaction_group");
        producer.setNamesrvAddr("124.220.170.254:9876");
        // 添加监听器
        producer.setTransactionListener(new 消息确认者());
        producer.start();
        for (int i = 1; i < 11; i++) {
            String topic = "TopicF";
            String tags = "transaction";
            String body = "Hello,TransactionMQProducer!This is " + i;
            Message msg = new Message(topic, tags, body.getBytes());
            // 人为的添加一个标签
            msg.setKeys("NUM:" + i);
            // 发送半事务消息
            TransactionSendResult sendResult = producer.sendMessageInTransaction(msg, null);
            log.info("sendResult:{}", sendResult);
        }
        // producer.shutdown();
    }
}
